package com.qsmx.core.webfile;

import android.os.Bundle;

import androidx.annotation.Nullable;

import android.view.View;

import com.qsmx.core.R;
import com.qsmx.core.databinding.DelegateWebViewBinding;
import com.qsmx.core.delegates.AtmosDelegate;
import com.qsmx.core.fusion.FusionCode;

public class WebViewDelegate extends AtmosDelegate {

    private DelegateWebViewBinding mBinding;
    private String mUrl = "";
    private String mTitle = "";
    private String mFrom = "";
    private boolean mParentIsBlack;
    private boolean isHasNoTitle;
    private WebDelegateImpl mDelegate;

    @Override
    public Object setLayout() {
        return R.layout.delegate_web_view;
    }

    public static WebViewDelegate create(String url, String title, boolean parentIsBlack) {

        final Bundle args = new Bundle();
        args.putString(FusionCode.WEB_URL, url);
        args.putString(FusionCode.WEB_TITLE, title);
        final WebViewDelegate delegate = new WebViewDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    public static WebViewDelegate create(String url, boolean hasTitle, boolean parentIsBlack) {

        final Bundle args = new Bundle();
        args.putString(FusionCode.WEB_URL, url);
        args.putBoolean(FusionCode.WEB_HAS_NO_TITLE, hasTitle);
        final WebViewDelegate delegate = new WebViewDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    public static WebViewDelegate create(String url, String title, String from, boolean parentIsBlack) {

        final Bundle args = new Bundle();
        args.putString(FusionCode.WEB_URL, url);
        args.putString(FusionCode.WEB_TITLE, title);
        args.putString(FusionCode.WEB_FROM, from);
        final WebViewDelegate delegate = new WebViewDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Bundle args = getArguments();
        if (args != null) {
            mUrl = args.getString(FusionCode.WEB_URL);
            mTitle = args.getString(FusionCode.WEB_TITLE);
            mFrom = args.getString(FusionCode.WEB_FROM);
            isHasNoTitle = args.getBoolean(FusionCode.WEB_HAS_NO_TITLE);
        } else {
            mUrl = "";
            mTitle = "";
            mFrom = "";
        }
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mDelegate = WebDelegateImpl.create(mUrl, mFrom, isHasNoTitle);
        mDelegate.setTopDelegate(this.getParentDelegate());
        mBinding = DelegateWebViewBinding.bind(rootView);
        mBinding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mDelegate != null) {
                    if (mDelegate.getWebView().canGoBack()) {
                        mDelegate.getWebView().goBack();
                    } else {
                        getSupportDelegate().pop();
                    }
                }
            }
        });

        getSupportDelegate().loadRootFragment(R.id.wv_web, mDelegate);

    }

    @Override
    public void onResume() {
        super.onResume();
    }
}
